package com.clhbolg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.clhbolg.domain.bo.ArticleBo;
import com.clhbolg.domain.vo.ArticleVo;
import com.clhbolg.model.dto.ArticleCardDTO;
import com.clhbolg.model.dto.ArticleDTO;
import com.clhbolg.model.dto.TopAndFeaturedArticlesDTO;
import com.clhbolg.model.es.ArticleSearchDTO;
import com.clhbolg.model.vo.request.ArticlePasswordVO;
import com.clhbolg.model.vo.request.ConditionVO;
import com.clhbolg.service.IArticleService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 文章
 *
 * @author cailinhu
 * @date 2023-02-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("articles")
public class ArticleController extends BaseController {

    private final IArticleService iArticleService;

    /**
     * 查询文章列表
     */
    @SaCheckPermission("blog-system:article:list")
    @GetMapping("/list")
    public TableDataInfo<ArticleVo> list(ArticleBo bo, PageQuery pageQuery) {
        return iArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章列表
     */
    @SaCheckPermission("blog-system:article:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArticleBo bo, HttpServletResponse response) {
        List<ArticleVo> list = iArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章", ArticleVo.class, response);
    }

    /**
     * 获取文章详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    @SaIgnore
    public R<ArticleDTO> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        ArticleDTO articleDTO=iArticleService.getArticleById(id);
        return R.ok(articleDTO);
    }


    /**
     * 文章密码校验接口
     * @param articlePasswordVO
     * @return
     */
    @PostMapping("/access")
    public R checkPassword(@Valid @RequestBody ArticlePasswordVO articlePasswordVO){
        iArticleService.accessArticle(articlePasswordVO);
        return R.ok();
    }
    /**
     * 新增文章
     */
    @SaCheckPermission("blog-system:article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArticleBo bo) {
        return toAjax(iArticleService.insertByBo(bo));
    }

    /**
     * 修改文章
     */
    @SaCheckPermission("blog-system:article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArticleBo bo) {
        return toAjax(iArticleService.updateByBo(bo));
    }

    /**
     * 获取置顶和推荐文章
     * @return
     */
    @GetMapping("/topAndFeatured")
    public R<TopAndFeaturedArticlesDTO> listTopAndFeaturedArticles() {
        return R.ok(iArticleService.listTopAndFeaturedArticles());
    }

    /**
     * 用户文章列表查询
     * @param vo
     * @return
     */
    @GetMapping("/all")
    public R<TableDataInfo<ArticleCardDTO>> ArticleList(ConditionVO vo, PageQuery pageQuery){
        TableDataInfo<ArticleCardDTO> list =iArticleService.userArticleList(vo,pageQuery);
        return R.ok(list);
    }

    @GetMapping("/categoryId")
    public TableDataInfo<ArticleCardDTO> categoryId(@RequestParam Integer categoryId,PageQuery pageQuery){
        return  iArticleService.listArticlesByCategoryId(categoryId,pageQuery);
    }

    /**
     * 删除文章
     *
     * @param ids 主键串
     */
    @SaCheckPermission("blog-system:article:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iArticleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 搜索文章
     *
     * @param condition
     * @return
     */
    @GetMapping("/search")
    @SaIgnore
    public R listArticleBySearch(ConditionVO condition){
        List<ArticleSearchDTO>  list=  iArticleService.listArticleBySearch(condition);
        return R.ok(list);
    }


    /**
     * 文章归档信息
     *
     * @return
     */
    @GetMapping("/archives/all")
    @SaIgnore
    public R<?> listArchives(PageQuery pageQuery) {
        return R.ok(iArticleService.listArchives(pageQuery));
    }

    @GetMapping("/tagId")
    @SaIgnore
    public  TableDataInfo<ArticleCardDTO> listByTag(PageQuery pageQuery,@RequestParam Integer tagId){
        return  iArticleService.getArticleByTag(pageQuery,tagId);
    }
}
